package com.zgjkhis.modules.system.rest;

import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.Schedule;
import com.zgjkhis.modules.system.service.ScheduleService;
import com.zgjkhis.modules.system.service.dto.ScheduleDto;
import com.zgjkhis.modules.system.service.dto.ScheduleQueryCriteria;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @date 2019-04-10
 */
@Api(tags = "系统：工作计划")
@RestController
@RequestMapping("/api/schedule")
public class ScheduleController {

    private static final String ENTITY_NAME = "schedule";
    private final ScheduleService scheduleService;

    public ScheduleController(ScheduleService scheduleService) {
        this.scheduleService = scheduleService;
    }

    @Log("分页查询工作计划")
    @ApiOperation("分页工作计划")
    @GetMapping(value = "/all")
    public ResponseEntity<Object> all() {
        return new ResponseEntity<>(scheduleService.queryAll(new ScheduleQueryCriteria()), HttpStatus.OK);
    }

    @Log("查询工作计划")
    @ApiOperation("查询工作计划")
    @GetMapping
    public ResponseEntity<PageResult<ScheduleDto>> getDicts(ScheduleQueryCriteria resources, Pageable pageable) {
        String pharmacyId = LoginUserUtil.getPharmId();
        resources.setPharmacyId(pharmacyId);
        return new ResponseEntity<>(scheduleService.queryAll(resources, pageable), HttpStatus.OK);
    }

    @Log("新增工作计划")
    @ApiOperation("新增工作计划")
    @PostMapping
    public ResponseEntity<Object> create(@Validated @RequestBody Schedule resources) {
        if (resources.getId() != null) {
            throw new ServiceException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        resources.setCreateTime(new Date());
        String pharmacyId = LoginUserUtil.getPharmId();
        resources.setPharmacyId(pharmacyId);
        resources.setCreateUserId(SecurityUtils.getUserId().toString());
        resources.setCreateUserName(SecurityUtils.getUsername());
        return new ResponseEntity<>(scheduleService.save(resources), HttpStatus.OK);
    }

    @Log("修改工作计划")
    @ApiOperation("修改工作计划")
    @PutMapping
    public ResponseEntity<Object> update(@Validated @RequestBody Schedule resources) {
        resources.setUpdateTime(new Date());
        if (resources.getStatus() == 2 || resources.getStatus() == 3) {
            resources.setCompleteTime(new Date());
        }
        scheduleService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("删除工作计划")
    @ApiOperation("删除工作计划")
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        scheduleService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
